package com.android.delivery.activity.fragment

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.print.PrintManager
import androidx.recyclerview.widget.RecyclerView
import com.android.delivery.R
import com.android.delivery.activity.PrintlnActivity
import com.android.delivery.adapter.LanJianedAdapter
import com.android.delivery.api.ApiService
import com.android.delivery.base.BaseListFragmet
import com.android.delivery.callback.DialogCallback
import com.android.delivery.callback.LzyResponse
import com.android.delivery.constant.Constants
import com.android.delivery.entity.Logistics
import com.android.delivery.entity.even.RefreshConfirmLanj
import com.android.delivery.entity.even.RefreshPrint
import com.android.delivery.util.ToastUtils
import com.android.delivery.util.print.service.ZPrinterManager
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.include_refresh_layout.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 揽件中心-已揽件列表
 */
class LanJedListFragment : BaseListFragmet<Logistics>() {

    private val jieDanAdapter: LanJianedAdapter by lazy {
        LanJianedAdapter()
    }


    companion object {
        fun newInstance(type: Int): LanJedListFragment {
            val fragment = LanJedListFragment()
            val bundle = Bundle()
            bundle.putInt(Constants.TYPE, type)
            fragment.arguments = bundle
            return fragment
        }
    }

    private var type: Int = 0

    override fun bindRecyclerView(recLv: RecyclerView?) {
        type = arguments!!.getInt(Constants.TYPE)

        recLv!!.adapter = jieDanAdapter
        jieDanAdapter.setOnItemChildClickListener() { adapter, view, position ->
            if (view.id == R.id.tv_1) {
                val item = jieDanAdapter.getItem(position)
                val connected = ZPrinterManager.getsManager(activity).workThread.isConnected
                if (connected){
                    ZPrinterManager.getsManager(activity).workThread.MyPrinter(item, mUiHandler)
                }else{
                    var intent = Intent(activity, PrintlnActivity::class.java)
                    intent.putExtra(Constants.ITEM, item)
                    startActivityForResult(intent, PrintlnActivity.REQUEST_CODE)
                }
            } else if (view.id == R.id.tv_2) {
                val item = jieDanAdapter.getItem(position)
                ApiService.printCloud(this, item!!.logisticsNo, object : DialogCallback<LzyResponse<Any>>() {
                    override fun onSuccess(response: Response<LzyResponse<Any>>?) {
                        if (response?.body()?.code == 60001){
                            var intent = Intent(activity, PrintlnActivity::class.java)
                            intent.putExtra(Constants.ITEM, item)
                            intent.putExtra(Constants.TYPE, PrintlnActivity.TAB_PRINT_CLOUD)
                            startActivityForResult(intent, PrintlnActivity.REQUEST_CODE)
                        }else{
                            ToastUtils.showShort(activity, "打印成功")
                            onRefresh(refreshLayout)
                            EventBus.getDefault().post(RefreshPrint())
                        }
                    }
                })
            }
        }

    }

    private val mUiHandler: Handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            if (msg.what == 1 && msg.obj != null) {
                val str = msg.obj as String
            }
        }
    }

    override fun loadData(page: Int, pageCount: Int) {
        ApiService.pick(this, page, type, object : DialogCallback<LzyResponse<Logistics>>() {
            override fun onSuccess(response: Response<LzyResponse<Logistics>>?) {
                onGetDataSuc(response!!.body().data.records)
            }
        })
    }

    override fun getData(): MutableList<Logistics> {
        return jieDanAdapter.data
    }

    override fun bindData(isRefresh: Boolean, list: MutableList<Logistics>?) {
        if (isRefresh) {
            jieDanAdapter.setNewData(list)
        } else {
            jieDanAdapter.addData(list!!)
        }
    }

    override fun isBindEventBus(): Boolean {
        return true
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun refresh(refresh: RefreshConfirmLanj) {
        onRefresh(refreshLayout)
    }
}